﻿using CommonTools;
using shop.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shop.Bll.BasicInfo
{
    public class StockWechatBll : IPageBll<StockWechat>
    {

        public string Title => "微信群设置";

        public bool EnableEditOnList => true;

        public bool EnableCreateOnList => true;

        public bool RefreshOnOpen => true;

        public Action<int> TurnPage { set; get; }

        public Func<object> GetPanelObject { get; set; }

        public StockWechat CopyItem(StockWechat item)
        {
            if (item == null)
                return null;

            var newItem = item.Clone();
            newItem.id = 0;
            newItem.isdel = false;

            return newItem;
        }

        public StockWechat CreateItem()
        {
            return new StockWechat() { isdel = false, FTime = DateTime.Now, };
        }

        public async Task<Result> DeleteItem(int id)
        {
            await DB.FSql.Update<StockWechat>()
                .Where(a => a.id == id && a.isdel == false)
                .Set(a => a.isdel, true)
                .ExecuteAffrowsAsync();


            return new Result(true);
        }


        public async Task<Result<int>> EditCell(string dataPropertyName, object value, StockWechat data)
        {
            if (data.id == 0)
            {
                data.FTime = await DB.FSql.GetTimeAsync();
                data.isdel = false;

                var nid = await DB.FSql.Insert(data)
                    .ExecuteIdentityAsync();

                data.id = (int)nid;
            }

            var dict = new Dictionary<string, object> { [dataPropertyName] = value };



            var dbRes = await DB.FSql.Update<StockWechat>()
                .Where(a => a.id == data.id && a.isdel == false)
                .SetDto(dict)
                .ExecuteAffrowsAsync();

            return new Result<int>(true, "!", data.id);
        }

        public IEnumerable<DeclareColumn> GetColumns()
        {
            return new[]
            {
                new DeclareColumn(nameof(StockWechat.id),"id"){ Visible= false},
                new DeclareColumn(nameof(StockWechat.Stock),"仓库"){ Width=200},
                new DeclareColumn(nameof(StockWechat.WechatGroup),"群名称"){ Width=260},
                new DeclareColumn(nameof(StockWechat.LogiName),"物流"){ Width=120},
                new DeclareColumn(nameof(StockWechat.Flag),"分类"){ Width=100, CellComboboxData=new []{"工厂","快递"} },
                new DeclareColumn(nameof(StockWechat.FTime),"登记时间"){Width=110, ReadOnly= true},
            };
        }

        public async Task<StockWechat> GetItem(int id)
        {
            return await DB.FSql.Select<StockWechat>()
                .Where(a => a.isdel == false && a.id == id)
                .ToOneAsync();
        }


        #region 查询参数

        private string _stock;
        private string _group;

        public event Action<object> OnFormShown;
        public event Action<object> OnFormClosed;
        public event Func<PageGridCellEditArg, Task> BeginEditCellAsync;

        #endregion

        /// <summary> 查询ui </summary>
        public virtual ControlBuilder GetUIBuilder()
        {
            return new ControlBuilder()
                .Add<string>(ControlEnum.guanTextBox, "仓库", c => _stock = c)
                .Add<string>(ControlEnum.guanTextBox, "群名称", c => _group = c)
                .AddQueryButton("查询");
        }

        /// <summary> 查询 </summary>
        public async Task<PageQueryResult<StockWechat>> QueryPage(DocFilterParameter queryPara)
        {
            var list = await DB.FSql.Select<StockWechat>()
                .Where(a => a.isdel == false)
                .WhereIf(!string.IsNullOrEmpty(_stock), a => a.Stock.Contains(_stock))
                .WhereIf(!string.IsNullOrEmpty(_group), a => a.WechatGroup.Contains(_group))
                .Page(queryPara.PageNo, queryPara.PageSize)
                //.OrderByDescending(a => a.shopGroup)
                .Count(out var c)
                .ToListAsync();

            return new PageQueryResult<StockWechat>(true, null, list, c);
        }



        public async Task<Result> SaveItem(StockWechat item)
        {
            if (item == null)
            {
                return new Result(false, "系统错误");
            }

            string err = string.Empty;
            if (string.IsNullOrEmpty(item.Stock))
            {
                err += "仓库不能为空!";
            }

            if (string.IsNullOrEmpty(err) == false)
            {
                return new Result(false, err);
            }

            item.isdel = false;
            item.FTime = await DB.FSql.GetTimeAsync();

            if (item.id <= 0)
            {
                var id = await DB.FSql.Insert(item)
                    .ExecuteIdentityAsync();

                item.id = (int)id;
            }
            else
            {
                await DB.FSql.Update<StockWechat>()
                    .SetSource(item)
                    .ExecuteAffrowsAsync();
            }

            return new Result(true);
        }


        public async Task<Result> SaveItem(IEnumerable<StockWechat> items)
        {
            if (items == null)
            {
                return new Result(false, "系统错误");
            }

            var time = await DB.FSql.GetTimeAsync();

            foreach (var item in items)
            {
                item.isdel = false;
                item.FTime = time;
            }

            await DB.FSql.Insert(items).ExecuteAffrowsAsync();

            return new Result(true);

        }




        public async Task<IEnumerable<StockWechat>> Query(string find)
        {
            var list = await DB.FSql.Select<StockWechat>()
                .Where(a => a.isdel == false)
                .WhereIf(!string.IsNullOrEmpty(find), a => a.Stock.Contains(find) || a.WechatGroup.Contains(find))
                .ToListAsync();

            return list;
        }





        public async Task<Result<object>> Save(StockWechat info)
        {
            if (info == null)
            {
                return new Result<object>(false, "系统错误");
            }

            string err = string.Empty;

            if (string.IsNullOrEmpty(info.Stock))
            {
                err = "仓库不能为空";
            }


            if (!string.IsNullOrEmpty(err))
            {
                return new Result<object>(false, err);
            }

            var bl = await DB.FSql.Select<StockWechat>().
                Where(a => a.isdel == false && a.Stock == info.Stock
                       && a.id != info.id)
                   .AnyAsync();

            if (bl)
            {
                return new Result<object>(false, "名称重复,不能使用!");
            }


            if (info.id <= 0)
            {
                var id = await DB.FSql.Insert(info).ExecuteIdentityAsync();

                info.id = (int)id;
            }
            else
            {
                info.isdel = false;

                await DB.FSql.Update<StockWechat>()
                    .SetSource(info)
                    .ExecuteAffrowsAsync();

            }
            return new Result<object>(true, info.id.ToString(), info.id);
        }


        public async Task<Result<object>> Delete(int id)
        {
            await DB.FSql.Update<StockWechat>()
               .Where(a => a.id == id)
               .Set(a => a.isdel, true)
               .ExecuteAffrowsAsync();

            return new Result<object>(true);
        }

        public void FireFormShown(object form)
        {
        }

        public void FireFormClosed(object form)
        {
        }

        public async Task<bool> OnBeginEditCell(Func<PageGridCellEditArg> getArgs)
        {
            return await Task.FromResult(false);
        }
    }
}
